/*
 * NativeCallsImplementation.java  0.4.0 / Nov 6, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.tools.ng.delegate;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;

import com.cute4j.tools.ng.AbstractNativeFile;
import com.cute4j.tools.ng.NativeGenerator;
import com.cute4j.tools.ng.Utils;

/**
 * Native Calls Implementation.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
class NativeCallsImplementation extends NativeImplementationBlock {

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new NativeCallsImplementation object.
   *
   * @param nativeFile the abstract native file
   * @param text the text
   * @param javaClass the java class
   */
  public NativeCallsImplementation(AbstractNativeFile nativeFile, String text,
                                   JavaClass javaClass) {
    super(nativeFile, text, javaClass);
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public void store(PrintWriter pw) throws IOException {
    String delegateObjectName = Utils.getDelegateObjectName(getJavaClass());
    ArrayList textMethods = getMethodImplementationsFromText();
    JavaMethod[] methods = getJavaClass().getMethods();
    boolean[] implementedMethods = new boolean[methods.length];
    for (int i = 0; i < methods.length; i++) {
      JavaMethod method = methods[i];
      if (method.isNative()) {
        String delegateObject = Utils.getNativeTagParameter(method,
                                                            "delegateObject");
        implementedMethods[i] =
            ((Utils.getNativeTagParameter(method, "code") != null) ||
             ((delegateObject != null) &&
              !delegateObjectName.equals(delegateObject)));
      }
    }
    if (getText().indexOf(" - Static Native Calls") >= 0) {
      Utils.printBlockComment(pw, Utils.getDelegateName(getJavaClass()) +
                              " - Static Native Calls.");
      for (int i = 0; i < methods.length; i++) {
        JavaMethod method = methods[i];
        if (!implementedMethods[i] && method.isNative() && method.isStatic()) {
          String methodImplementation =
                                 Utils.getDelegateMethodImplementation(method);
          int mdi = Utils.getMethodImplementationIndex(textMethods,
                                                       methodImplementation);
          if (mdi < 0) {
            pw.println(Utils.getDelegateMethodComment(method));
            pw.println(methodImplementation);
          } else {
            pw.println(textMethods.get(mdi));
            textMethods.remove(mdi);
          }
        }
      }
      for (int i = 0; i < methods.length; i++) {
        JavaMethod method = methods[i];
        if (!implementedMethods[i] && method.isNative() && !method.isStatic() &&
            "_create".equals(method.getName())) {
          String methodImplementation =
                  Utils.getNativeCallNewInstanceMethodImplementation(method);
          int mdi = Utils.getMethodImplementationIndex(textMethods,
                                                       methodImplementation);
          if (mdi < 0) {
            pw.println(Utils.getDelegateMethodComment(method));
            pw.println(methodImplementation);
          } else {
            pw.println(textMethods.get(mdi));
            textMethods.remove(mdi);
          }
        }
      }
    } else {
      Utils.printBlockComment(pw, Utils.getDelegateName(getJavaClass()) +
                              " - Native Calls.");
      for (int i = 0; i < methods.length; i++) {
        JavaMethod method = methods[i];
        if (!implementedMethods[i] && method.isNative() && !method.isStatic() &&
            !"_create".equals(method.getName())) {
          String methodImplementation =
                                    Utils.getDelegateMethodImplementation(method);
          int mdi = Utils.getMethodImplementationIndex(textMethods,
                                                       methodImplementation);
          if (mdi < 0) {
            pw.println(Utils.getDelegateMethodComment(method));
            pw.println(methodImplementation);
          } else {
            pw.println(textMethods.get(mdi));
            textMethods.remove(mdi);
          }
        }
      }
    }
    String newLine = NativeGenerator.getInstance().getNewLine();
    String methodStart = " */" + newLine;
    int methodStartLength = methodStart.length();
    String methodEnd = newLine + "}";
    int methodEndLength = methodEnd.length();
    for (int i = 0; i < textMethods.size(); i++) {
      StringBuffer methodText = new StringBuffer((String)textMethods.get(i));
      int startIndex = methodText.indexOf(methodStart);
      int endIndex = methodText.indexOf(methodEnd);
      if (methodText.charAt(startIndex + methodStartLength) == '/') {
        pw.println(methodText);
      } else {
        methodText = methodText.insert(endIndex + methodEndLength, "*/");
        methodText = methodText.insert(startIndex + methodStartLength, "/*");
        pw.println(methodText);
      }
    }
  }

}
